package com.lightsh.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;

/**
 * 该类在图片绘制时，进行了简单的处理
 * @author lightsh
 *
 */
public class RotateImageView extends ImageView{

	private static final String TAG = "RotateImageView";
	private static final boolean DEBUG = false;
	private int mBackgroundWidth = -1;
	private int mBackgroundHeight = -1;
	private Drawable mBackgroundDrawable;
	private Bitmap mBitmapFirst;
	private Bitmap mBitmapSecond;
	public RotateImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initBackgroundDrawable();
	}
	
	private void initBackgroundDrawable(){
		// FIXME 设置背景
		//mBackgroundDrawable = getContext().getResources().getDrawable(R.drawable.software_picture_border);
		mBackgroundWidth = mBackgroundDrawable.getIntrinsicWidth();
		mBackgroundHeight = mBackgroundDrawable.getIntrinsicHeight();
	}

	@Override
	public void setImageBitmap(Bitmap bitmap){
		mBitmapFirst = bitmap;
		if(mBitmapSecond == null){
			mBitmapSecond = bitmap;
		}
		invalidate();
	}
	
	public void setImageBitmap2(Bitmap bitmap){
		mBitmapSecond = bitmap;
		invalidate();
	}
	/**
	 * 背景框与图片的左右/上下间隔
	 */
	private static final int SRC_PADDING_L_R = 8;
	private static final int SRC_PADDING_T_B = 8;
	
	// 第二张图片的旋转角度
	private static final float ROTATE_VALUE = 2;

	@Override
	protected void onDraw(Canvas canvas) {
		log("onDraw");
		
		// 如果直接使用view的background,则会出现canvas已经画了背景的情况
		Drawable background = mBackgroundDrawable;
		if(background != null){
			background.setBounds(0, 0, mBackgroundWidth, mBackgroundHeight);
		}
		
		Paint paint = new Paint();
		paint.setAntiAlias(true);
		
		// 画第二张图片
		int count = canvas.getSaveCount();
		canvas.save();
		canvas.translate(22, 4);
		canvas.rotate(ROTATE_VALUE);
		if(background != null){
			background.draw(canvas);
		}
		if(mBitmapSecond != null){
			canvas.drawBitmap(mBitmapSecond, new Rect(0, 0, mBitmapSecond.getWidth(), mBitmapSecond.getHeight()), new Rect(SRC_PADDING_L_R, SRC_PADDING_T_B, mBackgroundWidth - SRC_PADDING_L_R, mBackgroundHeight - SRC_PADDING_T_B), paint);
		}		
		canvas.restoreToCount(count);
		
		// 画第一张图片
		count = canvas.getSaveCount();
		canvas.save();
		canvas.translate(0, 16);
		if(background != null){
			background.draw(canvas);
		}
		if(mBitmapFirst != null){
			canvas.drawBitmap(mBitmapFirst, new Rect(0, 0, mBitmapFirst.getWidth(), mBitmapFirst.getHeight()), new Rect(SRC_PADDING_L_R, SRC_PADDING_T_B, mBackgroundWidth - SRC_PADDING_L_R, mBackgroundHeight - SRC_PADDING_T_B), paint);
		}
		canvas.restoreToCount(count);
	}

	@Override
	protected void onAttachedToWindow() {
		super.onAttachedToWindow();
		
		log("onAttachedToWindow:" + mBackgroundWidth + "," + mBackgroundHeight);
	}

	@Override
	protected void onDetachedFromWindow() {
		log("onDetachedFromWindow");
		super.onDetachedFromWindow();
	}
	private void log(String msg){
		if(DEBUG){
			Log.e(TAG, msg);
		}
	}
}
